package com.jarvis.dubbo.filter;

import com.jarvis.dubbo.pojo.User;
import com.jarvis.dubbo.utils.UserHolder;
import org.apache.dubbo.rpc.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * TODO XXX
 *
 * @author liangjw
 * created in 2020/9/18
 */
public class TokenProviderFilter implements Filter {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String token = RpcContext.getContext().getAttachment("tokenString");
        System.out.println(String.format("Provider recieve a token(%s)", token));
        User user = new User(token, "1");
        UserHolder.setCurrentUser(user);
        Result result = invoker.invoke(invocation);
        UserHolder.clear();
        return result;
    }
}
